package com.fabriciojf.action;

import com.fabriciojf.controller.Action;
import com.fabriciojf.model.Category;
import com.fabriciojf.persistence.JpaUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action
 *
 * @author Fabricio S Costa fabriciojf@gmail.com
 * @since 01/12/2009
 * @version 1.0
 */
public class ManutencaoCategoriaAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {

            List<Category> categories = new ArrayList<Category>();
            String oql = "from " + Category.class.getName();
            categories = JpaUtil.getEntityManager().createQuery(oql).getResultList();

            request.setAttribute("categorias", categories);
            RequestDispatcher rd;
            rd = request.getRequestDispatcher("./adm/homeCategoria.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            response.sendRedirect("erro.jsp");
        }
    }
}
